<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>alg</title>
	<script>
		var sortFn = {
			insert: function(arr){
				// var arr = [6, 2, 4, 1, 5, 9];
				// console.log("Insert Sort",[6, 2, 4, 1, 5, 9]);

				for(var i = 1; i < arr.length; i++){
					var j = i;
					var cur = arr[i];
					if(arr[i - 1] > arr[i]){
						console.log("j = %d 可移动",j);
						while(j >= 0 && arr[j - 1] > cur){
							arr[j] = arr[j - 1];
							console.log("while,j = %d",j,arr);
							j--;
						}
						console.log("end while j = %d, i = %d, cur = %d",j,i,cur);
						arr[j] = cur;
						console.log("move->",arr);
					}
				}
				return arr;
			},
			selection: function(arr) {
				for(var i = 0; len = arr.length, i < len; i++){
					var cur = arr[i];
				}
			}
		}

		window.onload = function() {
			var btn = document.querySelector("#btn-sort");
			btn.onclick = function() {
				var s = document.querySelector("#unsort").value;
				var re = /\d/g;
				var arr = [];
				s.replace(re,function(i,j,k){
					arr.push(parseInt(i));
				});
				var r = sortFn.insert(arr);
				document.querySelector("#sorted")
					.value = r.join(",");
			}
		}

		
	</script>
</head>
<body>
	<input type="text" id="unsort" style="width:500px;" value="1,2,4,5,6,9"/> <br/>
	<input type="button" value="排序" id="btn-sort"><br/>
	<input type="text" id="sorted" style="width:500px;"/> <br/>

	<div id="msg"></div>

	
</body>
</html>